package com.base.utils;

import java.util.HashMap;
import java.util.Map;

public class MineUtils {

	private static Map<String, String> mineMap = new HashMap<String, String>();//扩展名和MINE类型对应表
	static{
		mineMap.put(null, "application/octet-stream");
		mineMap.put("acx","application/internet-property-stream");
		mineMap.put("ai","application/postscript");
		mineMap.put("aif","audio/x-aiff");
		mineMap.put("aifc","audio/x-aiff");
		mineMap.put("aiff","audio/x-aiff");
		mineMap.put("asf","video/x-ms-asf");
		mineMap.put("asr","video/x-ms-asf");
		mineMap.put("asx","video/x-ms-asf");
		mineMap.put("au","audio/basic");
		mineMap.put("avi","video/x-msvideo");
		mineMap.put("axs","application/olescript");
		mineMap.put("bas","text/plain");
		mineMap.put("bcpio","application/x-bcpio");
		mineMap.put("bin","application/octet-stream");
		mineMap.put("bmp","image/bmp");
		mineMap.put("c","text/plain");
		mineMap.put("cat","application/vnd.ms-pkiseccat");
		mineMap.put("cdf","application/x-cdf");
		mineMap.put("cer","application/x-x509-ca-cert");
		mineMap.put("class","application/octet-stream");
		mineMap.put("clp","application/x-msclip");
		mineMap.put("cmx","image/x-cmx");
		mineMap.put("cod","image/cis-cod");
		mineMap.put("cpio","application/x-cpio");
		mineMap.put("crd","application/x-mscardfile");
		mineMap.put("crl","application/pkix-crl");
		mineMap.put("crt","application/x-x509-ca-cert");
		mineMap.put("csh","application/x-csh");
		mineMap.put("css","text/css");
		mineMap.put("dcr","application/x-director");
		mineMap.put("der","application/x-x509-ca-cert");
		mineMap.put("dir","application/x-director");
		mineMap.put("dll","application/x-msdownload");
		mineMap.put("dms","application/octet-stream");
		mineMap.put("doc","application/msword");
		mineMap.put("dot","application/msword");
		mineMap.put("docx","application/msword");
		mineMap.put("dotx","application/msword");
		mineMap.put("dvi","application/x-dvi");
		mineMap.put("dxr","application/x-director");
		mineMap.put("eps","application/postscript");
		mineMap.put("etx","text/x-setext");
		mineMap.put("evy","application/envoy");
		mineMap.put("exe","application/octet-stream");
		mineMap.put("fif","application/fractals");
		mineMap.put("flr","x-world/x-vrml");
		mineMap.put("gif","image/gif");
		mineMap.put("gtar","application/x-gtar");
		mineMap.put("gz","application/x-gzip");
		mineMap.put("h","text/plain");
		mineMap.put("hdf","application/x-hdf");
		mineMap.put("hlp","application/winhlp");
		mineMap.put("hqx","application/mac-binhex40");
		mineMap.put("hta","application/hta");
		mineMap.put("htc","text/x-component");
		mineMap.put("htm","text/html");
		mineMap.put("html","text/html");
		mineMap.put("htt","text/webviewhtml");
		mineMap.put("ico","image/x-icon");
		mineMap.put("ief","image/ief");
		mineMap.put("iii","application/x-iphone");
		mineMap.put("ins","application/x-internet-signup");
		mineMap.put("isp","application/x-internet-signup");
		mineMap.put("jfif","image/pipeg");
		mineMap.put("jpe","image/jpeg");
		mineMap.put("jpeg","image/jpeg");
		mineMap.put("jpg","image/jpeg");
		mineMap.put("js","application/x-javascript");
		mineMap.put("latex","application/x-latex");
		mineMap.put("lha","application/octet-stream");
		mineMap.put("lsf","video/x-la-asf");
		mineMap.put("lsx","video/x-la-asf");
		mineMap.put("lzh","application/octet-stream");
		mineMap.put("m13","application/x-msmediaview");
		mineMap.put("m14","application/x-msmediaview");
		mineMap.put("m3u","audio/x-mpegurl");
		mineMap.put("man","application/x-troff-man");
		mineMap.put("mdb","application/x-msaccess");
		mineMap.put("me","application/x-troff-me");
		mineMap.put("mht","message/rfc822");
		mineMap.put("mhtml","message/rfc822");
		mineMap.put("mid","audio/mid");
		mineMap.put("mny","application/x-msmoney");
		mineMap.put("mov","video/quicktime");
		mineMap.put("movie","video/x-sgi-movie");
		mineMap.put("mp2","video/mpeg");
		mineMap.put("mp3","audio/mpeg");
		mineMap.put("mpa","video/mpeg");
		mineMap.put("mpe","video/mpeg");
		mineMap.put("mpeg","video/mpeg");
		mineMap.put("mpg","video/mpeg");
		mineMap.put("mpp","application/vnd.ms-project");
		mineMap.put("mpv2","video/mpeg");
		mineMap.put("ms","application/x-troff-ms");
		mineMap.put("mvb","application/x-msmediaview");
		mineMap.put("nws","message/rfc822");
		mineMap.put("oda","application/oda");
		mineMap.put("p10","application/pkcs10");
		mineMap.put("p12","application/x-pkcs12");
		mineMap.put("p7b","application/x-pkcs7-certificates");
		mineMap.put("p7c","application/x-pkcs7-mime");
		mineMap.put("p7m","application/x-pkcs7-mime");
		mineMap.put("p7r","application/x-pkcs7-certreqresp");
		mineMap.put("p7s","application/x-pkcs7-signature");
		mineMap.put("pbm","image/x-portable-bitmap");
		mineMap.put("pdf","application/pdf");
		mineMap.put("pfx","application/x-pkcs12");
		mineMap.put("pgm","image/x-portable-graymap");
		mineMap.put("pko","application/ynd.ms-pkipko");
		mineMap.put("pma","application/x-perfmon");
		mineMap.put("pmc","application/x-perfmon");
		mineMap.put("pml","application/x-perfmon");
		mineMap.put("pmr","application/x-perfmon");
		mineMap.put("pmw","application/x-perfmon");
		mineMap.put("png","image/png");
		mineMap.put("pnm","image/x-portable-anymap");
		mineMap.put("pot","application/vnd.ms-powerpoint");
		mineMap.put("potx","application/vnd.ms-powerpoint");
		mineMap.put("ppm","image/x-portable-pixmap");
		mineMap.put("pps","application/vnd.ms-powerpoint");
		mineMap.put("ppsx","application/vnd.ms-powerpoint");
		mineMap.put("ppt","application/vnd.ms-powerpoint");
		mineMap.put("pptx","application/vnd.ms-powerpoint");
		mineMap.put("prf","application/pics-rules");
		mineMap.put("ps","application/postscript");
		mineMap.put("pub","application/x-mspublisher");
		mineMap.put("qt","video/quicktime");
		mineMap.put("ra","audio/x-pn-realaudio");
		mineMap.put("ram","audio/x-pn-realaudio");
		mineMap.put("rar", "application/x-rar-compressed");
		mineMap.put("ras","image/x-cmu-raster");
		mineMap.put("rgb","image/x-rgb");
		mineMap.put("rmi","audio/mid");
		mineMap.put("roff","application/x-troff");
		mineMap.put("rtf","application/rtf");
		mineMap.put("rtx","text/richtext");
		mineMap.put("scd","application/x-msschedule");
		mineMap.put("sct","text/scriptlet");
		mineMap.put("setpay","application/set-payment-initiation");
		mineMap.put("setreg","application/set-registration-initiation");
		mineMap.put("sh","application/x-sh");
		mineMap.put("shar","application/x-shar");
		mineMap.put("sit","application/x-stuffit");
		mineMap.put("snd","audio/basic");
		mineMap.put("spc","application/x-pkcs7-certificates");
		mineMap.put("spl","application/futuresplash");
		mineMap.put("src","application/x-wais-source");
		mineMap.put("sst","application/vnd.ms-pkicertstore");
		mineMap.put("stl","application/vnd.ms-pkistl");
		mineMap.put("stm","text/html");
		mineMap.put("svg","image/svg+xml");
		mineMap.put("sv4cpio","application/x-sv4cpio");
		mineMap.put("sv4crc","application/x-sv4crc");
		mineMap.put("swf","application/x-shockwave-flash");
		mineMap.put("t","application/x-troff");
		mineMap.put("tar","application/x-tar");
		mineMap.put("tcl","application/x-tcl");
		mineMap.put("tex","application/x-tex");
		mineMap.put("texi","application/x-texinfo");
		mineMap.put("texinfo","application/x-texinfo");
		mineMap.put("tgz","application/x-compressed");
		mineMap.put("tif","image/tiff");
		mineMap.put("tiff","image/tiff");
		mineMap.put("tr","application/x-troff");
		mineMap.put("trm","application/x-msterminal");
		mineMap.put("tsv","text/tab-separated-values");
		mineMap.put("txt","text/plain");
		mineMap.put("uls","text/iuls");
		mineMap.put("ustar","application/x-ustar");
		mineMap.put("vcf","text/x-vcard");
		mineMap.put("vrml","x-world/x-vrml");
		mineMap.put("wav","audio/x-wav");
		mineMap.put("wcm","application/vnd.ms-works");
		mineMap.put("wdb","application/vnd.ms-works");
		mineMap.put("wks","application/vnd.ms-works");
		mineMap.put("wmf","application/x-msmetafile");
		mineMap.put("wps","application/vnd.ms-works");
		mineMap.put("wri","application/x-mswrite");
		mineMap.put("wrl","x-world/x-vrml");
		mineMap.put("wrz","x-world/x-vrml");
		mineMap.put("xaf","x-world/x-vrml");
		mineMap.put("xbm","image/x-xbitmap");
		mineMap.put("xla","application/vnd.ms-excel");
		mineMap.put("xlax","application/vnd.ms-excel");
		mineMap.put("xlc","application/vnd.ms-excel");
		mineMap.put("xlm","application/vnd.ms-excel");
		mineMap.put("xls","application/vnd.ms-excel");
		mineMap.put("xlsb","application/vnd.ms-excel");
		mineMap.put("xlsx","application/vnd.ms-excel");
		mineMap.put("xlt","application/vnd.ms-excel");
		mineMap.put("xltx","application/vnd.ms-excel");
		mineMap.put("xlw","application/vnd.ms-excel");
		mineMap.put("xof","x-world/x-vrml");
		mineMap.put("xpm","image/x-xpixmap");
		mineMap.put("xwd","image/x-xwindowdump");
		mineMap.put("z","application/x-compress");
		mineMap.put("zip","application/zip");
	}
	
	/**
	 * 根据文件名获得后缀扩展名,如果没有，则返回null,否则以小写返回
	 * @param fileName
	 * @return
	 */
	public static String getSuffix(String fileName){
		int index = fileName.lastIndexOf('.');
		if(index>0){
			return fileName.substring(index+1).toLowerCase();
		}else{
			return null;
		}
	}
	
	/**
	 * 根据扩展名获得匹配的MIME类型，没有找到就返回默认的
	 * @param extension
	 * @return
	 */
	public static String getMineBySuffix(String suffix){
		if(suffix!=null){
			suffix = suffix.toLowerCase();
		}
		String result = mineMap.get(suffix);
		if(result==null){
			result = mineMap.get(null);
		}
		return result;
	}
	
	/**
	 * 根据文件名获得MINE类型
	 * @param fileName
	 * @return
	 */
	public static String getMineByFilename(String fileName){
		return getMineBySuffix(getSuffix(fileName));
	}
}
